#include "head.h"
#include "cc9d_support.h"
#include "adc.h"
#include "App_adc.h"
#include <stdio.h>


#define LOCAL_PORT AdcApp_PORT

static uint32 ms_ticks = 0;//1ms计数
static int32 last_adc_value = 0;//上一次的ADC值
static int32 adc_report_threshold = 15;//ADC值变化的阈值，超过此值则发送一个包报告ADC值。
static uint16 hart_beat_ticks = 10000;//每100ms进行一个计数，到100时发送一个包，并重新计数。
static uint16 hart_beat_interval = 100;//心跳包的最大计数，10s发送一个心跳包。

static uint16 remote_addr = 0x0000; //远程地址
static uint8 to_port = 32; //目标端口 

//ADC报告函数，占用32端口，用于发送ADC采集到的数据。
static void sendAdcPkg(int32 adc_value) {
    sCc9dPkg send_pkg;
    send_pkg.from_port = LOCAL_PORT;
    send_pkg.to_port = to_port;
    send_pkg.remote_addr = remote_addr;
    send_pkg.datas[0] = (adc_value >> 0) & 0xff;
    send_pkg.datas[1] = (adc_value >> 8) & 0xff;
    send_pkg.datas[2] = (ms_ticks >> 0) & 0xff;
    send_pkg.datas[3] = (ms_ticks >> 8) & 0xff;
    send_pkg.datas[4] = (ms_ticks >> 16) & 0xff;
    send_pkg.datas[5] = (ms_ticks >> 24) & 0xff;
    cc9d_SendPkg(&send_pkg, 6);
}




void AdcApp_recvPkg(sCc9dPkg* recv_pkg, uint8 data_len) {
    if (data_len == 0) {
        return;
    }
    if (recv_pkg->datas[0] == 'r') {//发送r立即读取一个ADC值
        hart_beat_ticks = 10000;
    } else if (recv_pkg->datas[0] == 'i') {//发送i后面跟随ADC采样间隔，设置心跳包的时间间隔
        if (data_len != 3) {
            printf("ADC report interval set error, data_len = %d\n", data_len);
            return;
        }
        uint16 interval = recv_pkg->datas[2];
        interval <<= 8;
        interval += recv_pkg->datas[1];
        printf("ADC report interval set to (%d x 100) ms\n", interval);
        hart_beat_interval = interval;
    } else if (recv_pkg->datas[0] == 't') {//发送t后面跟随ADC阈值，设置ADC报告的阈值
        if (data_len != 2) {
            printf("ADC report threshold set error, data_len = %d\n", data_len);
            return;
        }
        adc_report_threshold = recv_pkg->datas[1];
        if (adc_report_threshold < 5) {
            adc_report_threshold = 5;
        }
        printf("ADC report threshold set to %d\n", adc_report_threshold);
    }else if(recv_pkg->datas[0] == 'T'){//报告当前的阈值
        sCc9dPkg send_pkg;
        send_pkg.from_port = LOCAL_PORT;
        send_pkg.to_port = recv_pkg->from_port;
        send_pkg.remote_addr = recv_pkg->remote_addr;
        send_pkg.datas[0] = 'T';
        send_pkg.datas[1] = adc_report_threshold;
        cc9d_SendPkg(&send_pkg, 2);
    }else if(recv_pkg->datas[0] == 'I'){//报告当前的心跳包间隔
        sCc9dPkg send_pkg;
        send_pkg.from_port = LOCAL_PORT;
        send_pkg.to_port = recv_pkg->from_port;
        send_pkg.remote_addr = recv_pkg->remote_addr;
        send_pkg.datas[0] = 'I';
        send_pkg.datas[1] = (hart_beat_interval >> 0) & 0xff;
        send_pkg.datas[2] = (hart_beat_interval >> 8) & 0xff;
        cc9d_SendPkg(&send_pkg, 3);
    }
}

//Adc循环读取函数，每1ms被调用一次，每100ms读取一次ADC值。
//如果ADC值发生了大于15的变化，则发送一个包，报告ADC值。
//如果ADC值一直没有变化，则每10s发送一个包，报告ADC值。同时作为心跳包。
void AdcApp_msTick() {
    ms_ticks++;
    if (ms_ticks % 100 == 0) {
        int32 adc_value = Adc_getValue(0);
        int32 diff = adc_value - last_adc_value;
        if ((diff > adc_report_threshold) || (diff < -adc_report_threshold) 
        ||  (hart_beat_ticks >= hart_beat_interval)
        ) {
            sendAdcPkg(adc_value);
            last_adc_value = adc_value;
            hart_beat_ticks = 0;
        } else {
            hart_beat_ticks++;
        }
    }
}

void AdcApp_init(){
    Adc_init();
}

